/**
 * 功能描述
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
    module.exports = {
    	save:function(widget){
    		if(widget.get("form").valid()){
    			var topformData = widget.get("topform").getData();
    			var formData = widget.get("form").getData();
    			var editgridData = widget.get("editgrid").getData();
    			
    			var checkOutNum = 0;
    			var checkOut = false;
    			var list = [];
    			var isAlert = false;
    			for(var i=0;i<editgridData.length;i++){
    				if(editgridData[i].signUpRegister.checkOut.key == "true"){
    					checkOutNum++;
    				}
    				
    				if(editgridData[i].signUpRegister.checkOut.key == "true" && !editgridData[i].signUpRegister.checkOutDate){
    					isAlert = true;
    				}
    				
    				list.push({
    					"pkRoomPersonalInfo":editgridData[i].pkRoomPersonalInfo,
    					"signUpRegister.pkSignUpRegister":editgridData[i].signUpRegister.pkSignUpRegister,
    					"signUpRegister.checkOutDate":editgridData[i].signUpRegister.checkOutDate,
    					"signUpRegister.checkOut":editgridData[i].signUpRegister.checkOut.key
    				});
    			}
    			
    			if(isAlert){
    				Dialog.alert({
						content:"退房日期不能为空"
        			});
    				return;
    			}
    			
    			if(checkOutNum == editgridData.length){
    				checkOut = true;
    			}
    			
    			var data = {
    				"pkCheckIn":topformData.pkCheckIn,
    				"checkOut":checkOut,
    				"checkOutWaterMeter":formData["roomRegister.waterMeter"] == "" ? 0 : parseFloat(formData["roomRegister.waterMeter"]),
    				"checkOutElectricMeter":formData["roomRegister.electricMeter"] == "" ? 0 : parseFloat(formData["roomRegister.electricMeter"]),
    				"list":list
    			};
    			
    			data = aw.customParam(data);
        		
//        		aw.saveOrUpdate("api/checkin/checkout",data,function(data){
//        			
//        			Dialog.alert({
//        				content:"保存成功"
//        			});
//        			widget.hide([".J-form", ".J-editgrid",".J-topform"]).show(".J-grid");
//					widget.get("subnav").hide(["save", "cancel"]).show(["servicePoint", "checkOutStatus", "travelDate","buildings"]);
//					widget.get("grid").refresh();
//    			});
    		}
    	},
    	loadBuilding:function(widget){
    		aw.ajax({
				url:"api/orgbuilding/query",
				data:{
					servicePoint:widget.get("subnav").getValue("servicePoint"),
					fetchProperties:"pkOrgBuilding,name"
				},
				dataType:"json",
				success:function(data){
					widget.get("subnav").setData("buildings",data);
					widget.get("grid").refresh();
				}
			});
    	},
    	consumpData:function(widget){
    		var roomConsumptionData = widget.get("roomConsumpEdit").getData();
    		var damagesConsumptionData = widget.get("damagesConsumpEdit").getData();
    		var consumptionData = [];
    		for(var i=0; i < roomConsumptionData.length; i++){
    			if(typeof(roomConsumptionData[i].consumptionType) != "string"){
    				roomConsumptionData[i].consumptionType = roomConsumptionData[i].consumptionType.key;
    			};
    			if(typeof(roomConsumptionData[i].goods) != "string"){
    				roomConsumptionData[i].goods = roomConsumptionData[i].goods.pkGoods;
    			};
    			consumptionData.push(roomConsumptionData[i]);
    		}
    		for(var i=0; i < damagesConsumptionData.length; i++){
    			if(typeof(damagesConsumptionData[i].consumptionType) != "string"){
    				damagesConsumptionData[i].consumptionType = damagesConsumptionData[i].consumptionType.key;
    			};
    			if(typeof(damagesConsumptionData[i].goods) != "string"){
    				damagesConsumptionData[i].goods = damagesConsumptionData[i].goods.pkGoods;
    			};
    			consumptionData.push(damagesConsumptionData[i]);
    		}
    		var data = {
    			pkRoomPersonalInfo:widget.get("pkRoomPersonalInfo"),
    			consumptionList: consumptionData
    		};
    		aw.ajax({
				url:"api/roomperconsumption/saveconsump",
				data:aw.customParam(data),
				dataType:"json",
				success:function(datas){
					widget.show([".J-form",".J-topform", ".J-editgrid"]).hide(".J-consumpeditgrid");
					widget.get("subnav").hide(["save","return"]).show("cancel").hide(["servicePoint", "checkOutStatus", "travelDate","buildings"]);
					widget.get("subnav").setTitle("退房查房");
					widget.handler.gridHandler.roomPerGridRefresh(widget);
				}
			});
    	}
    };
});
